package cn.mrlong.base.ktnet.header

import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.ResponseBody

/**
 * 作者：BINGO
 * 时间：2021/03/26 14:59
 *
 * 去掉默认的user-agent, 可能被服务器拦截
 */
class NetInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()
        //移除默认参数
        request = request.newBuilder()
            .removeHeader("User-Agent")
            .removeHeader("Accept-Encoding")
            .build();
        val response = chain.proceed(request)
        return if (response.body != null && response.body!!.contentType() != null) {
            val mediaType = response.body!!.contentType()
            val content = response.body!!.string()
            val responseBody = ResponseBody.create(mediaType, content)
            response.newBuilder().body(responseBody).build()
        } else {
            response
        }
    }
}